﻿

#include "utilityMpiWrapper.hpp"

namespace {
using namespace UTILITY;

bool initialized_ = false;

class InitCommRegister {
 public:
  InitCommRegister() { initialized_ = true; }

  ~InitCommRegister() {
    if (initialized_) {
      COMM::stop();
    }
  }
};

}  // namespace

namespace UTILITY {

CommunicationManager *COMM::globalManager = NULL;
bool COMM::createLogFile_ = true;

void COMM::init(int *argc, char ***argv, const char *type) {
  if (initialized_ == false)
    static InitCommRegister reg;
  else {
    COMM::pErr() << "!Proc[" << COMM::getGlobalId() << "]: " << '\n'
                 << __FILE__ << " +" << __LINE__ << __FUNCTION__ << ": " << '\n'
                 << "Error: ";

    exit(-1);
  }

  COMM::globalManager = CommunicationManager::New(argc, argv, type);
  COMM::globalManager->createLogFile(createLogFile_);
}

void COMM::stop() {
  if (COMM::globalManager != NULL) {
    delete COMM::globalManager;
  }
}
void COMM::createLogFile(bool TorF) { createLogFile_ = TorF; }

}  // namespace UTILITY
